
//
//  UpdatePolicyManager.m
//  zhijiansuzhou
//
//  Created by 度周末网络-王腾 on 16/5/24.
//  Copyright © 2016年 Suzhou fish Information Technology Co., Ltd. All rights reserved.
//

#import "UpdatePolicyManager.h"



@implementation UpdatePolicyManager

+(instancetype)sharedManager {
    static dispatch_once_t onceToken;
    static UpdatePolicyManager *instance;
    dispatch_once(&onceToken, ^{
        instance = [[UpdatePolicyManager alloc] init];
    });
    return instance;
}

-(void)updatePushStatus:(NSString *)pushStatus{
    
    
    [[NSUserDefaults standardUserDefaults] setObject:pushStatus forKey:@"lastUpdateBy"];
  
}

/**
 *  保存更新策略数据
 *
 *  @param dicData
 */
-(void)saveUpdatePolicyData:(NSDictionary *)dicData{
    
   dispatch_async(dispatch_get_main_queue(), ^{
       if ([[dicData objectForKey:@"data"] isKindOfClass:[NSNull class]]) {
           return;
       }
       NSMutableDictionary *updateDic = [[NSMutableDictionary alloc ] init];
       
       NSDictionary *dic = [[[dicData objectForKey:@"data"]  objectForKey:@"versionList"] firstObject];
       
       [updateDic setObj:[[dicData objectForKey:@"data"] objectForKey:@"needUpdate"] forKey:@"needUpdate"];
       
       
       NSString *updateMsg = [[dicData objectForKey:@"data"] objectForKey:@"updateMsg"];
       if (![updateMsg isKindOfClass:[NSNull class]]) {
           [updateDic setObj:updateMsg forKey:@"updateMsg"];
       }
       
      
       [updateDic setObj:[dic objectForKey:@"versionLink"] forKey:@"versionLink"];
       
       NSDictionary *data = [dicData objectForKey:@"data"];
       
       if (data) {
           NSDictionary *m    = [data objectForKey:@"m"];
           if (![m isKindOfClass:[NSNull class]]) {
               
               
               NSString *s = [m objectForKey:@"lastUpdateBy"];
               if (![s isKindOfClass:[NSNull class]]) {
                   [[NSUserDefaults standardUserDefaults]setObject: [m objectForKey:@"lastUpdateBy"] forKey:@"lastUpdateBy"];
               }
               
               
               
           }
           
       }
       
       
       
       [self saveDishFile:@"updatePolicyCache" withDic:updateDic];
       
       [self setUpUpdatePolicy];
   });
    
  
    
}

/**
 *  处理更新策略
 */
-(void)setUpUpdatePolicy{
    UpdatePolicyEntity *entity = [self loadUpdatePolicyData];

    // 按需更新
    if ([entity.needUpdate isEqualToString:@"1"]) {
        
    
        DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"提示" contentText:[NSString stringWithFormat:@"\n%@",entity.updateMsg] leftButtonTitle:@"更新" rightButtonTitle:@"取消"];
        [alert show];
        alert.leftBlock = ^() {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:entity.versionLink]];
        };
    }
    // 强制更新
    else if ([entity.needUpdate isEqualToString:@"2"]){
        DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"提示" contentText:[NSString stringWithFormat:@"\n%@",entity.updateMsg] leftButtonTitle:nil rightButtonTitle:@"更新"];
        [alert show];
        alert.rightBlock = ^() {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:entity.versionLink]];
        };
    }
}


-(UpdatePolicyEntity *)loadUpdatePolicyData{
    
   NSMutableDictionary *dic = [self loadDishFile:@"updatePolicyCache"];
    UpdatePolicyEntity *entity = [[UpdatePolicyEntity alloc ] init];
    entity.needUpdate  = [dic objectForKey:@"needUpdate"];
    entity.updateMsg   = [dic objectForKey:@"updateMsg"];
    entity.versionLink = [dic objectForKey:@"versionLink"];
    entity.lastUpdateBy = [[NSUserDefaults standardUserDefaults]objectForKey:@"lastUpdateBy"];
    
    return entity;
}

-(void)saveDishFile:(NSString *)fileName withDic:(NSMutableDictionary *)dic
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
    
    
  
    [dic writeToFile:filename atomically:YES];
}

-(NSMutableDictionary *)loadDishFile:(NSString *)fileName
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:filename];
    
    return dic;
}


@end

@implementation UpdatePolicyEntity



@end
